import { inject } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs';
import { bus } from 'wujie';

export function initialWuJieRouter() {
  let router = inject(Router);
  let url = '';
  router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event) => {
    if (url !== event.urlAfterRedirects) {
      url = event.urlAfterRedirects;
      bus.$emit('jump', {from: bus['id'], path: event.urlAfterRedirects});
    }
  });
  bus.$on('jump', (args: any) => {
    if (args.from !== bus['id']) {
      router.navigate([args.path]).then();
    }
  });
}
